﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using GameStateManagement;

namespace PiratesOfBermuda
{
    public enum PlayerType { Single, Host, Client};

    public class Player : Ship
    {
        protected int _lifeRemained;

        public int LifeRemained
        {
            get { return _lifeRemained; }
            set { _lifeRemained = value; }
        }

        internal Cannon cannon;
        public bool IsCannonAnimating { get { return cannon.IsAnimating; } }

        Texture2D whirlInTexture;
        Texture2D whirlOutTexture;

        Animation whirlInAnimation;
        Animation whirlOutAnimation;

        bool isWhriling = false;

        public Player(Game game, SpriteBatch spriteBatch)
            : base(game, spriteBatch)
        {
            _name = PlayerType.Single.ToString();
        }

        public override void Initialize()
        {
            base.Initialize(@"Textures/MainGameplay/goodship", new Rectangle(0, 0, 50, 50), new Point(1, 8), new Point(5, 5));
            whirlInTexture = currentGame.Content.Load<Texture2D>(@"Textures/MainGameplay/goodshipwhirlin");
            whirlOutTexture = currentGame.Content.Load<Texture2D>(@"Textures/MainGameplay/goodshipwhirlout");
            base.Initialize();
        }

        public void InitializeClient()
        {
            Initialize();
            Texture = currentGame.Content.Load<Texture2D>(@"Textures/MainGameplay/blueship");
            whirlInTexture = currentGame.Content.Load<Texture2D>(@"Textures/MainGameplay/goodshipwhirlinBlue");
            whirlOutTexture = currentGame.Content.Load<Texture2D>(@"Textures/MainGameplay/goodshipwhirloutBlue");
        }

        public void MoveAndWhirl(Point dest1, Point dest2)
        {
            base.Move(dest1.X, dest1.Y);
            isWhriling = true;
            whirlInAnimation = new Animation(whirlInTexture, new Point(50, 50), new Point(32, 1), dest1);
            whirlInAnimation.PlayFromFrameIndex(0);
            whirlOutAnimation = new Animation(whirlOutTexture, new Point(50, 50), new Point(32, 1), dest2);
            whirlOutAnimation.PlayFromFrameIndex(0);
            this.newX = dest2.X;
            this.newY = dest2.Y;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (CurrentShipState == ShipState.Standby && movingAnimation.Count == 0 && isWhriling)
            {
                CurrentShipState = ShipState.WhirlIn;
                AudioManager.PlaySound("WhirlIn");
            }
            else if (CurrentShipState == ShipState.WhirlIn)
            {
                if (whirlInAnimation.IsActive)
                {
                    whirlInAnimation.Update();
                }
                else 
                {
                    CurrentShipState = ShipState.WhirlOut;
                    AudioManager.PlaySound("WhirlOut");
                }
            }
            else if (CurrentShipState == ShipState.WhirlOut)
            {
                if (whirlOutAnimation.IsActive)
                {
                    whirlOutAnimation.Update();
                }
                else
                {
                    isWhriling = false;
                    CurrentShipState = ShipState.Standby;
                    IsActive = false;
                    X = newX;
                    Y = newY;
                }
            }
            else if (CurrentShipState == ShipState.Attacking)
            {
                cannon.Update();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (CurrentShipState == ShipState.WhirlIn || CurrentShipState == ShipState.WhirlOut)
            {
                if (whirlInAnimation.IsActive)
                    whirlInAnimation.Draw(spriteBatch);
                else if (whirlOutAnimation.IsActive)
                    whirlOutAnimation.Draw(spriteBatch);
            }
            else if (CurrentShipState == ShipState.Attacking)
            {
                cannon.Draw(gameTime);
            }
            base.Draw(gameTime);
        }
        
        public void Fire(Point? p1, bool is1Exploded, Point? p2, bool is2Exploded)
        {
            CurrentShipState = ShipState.Attacking;
            cannon = new Cannon(currentGame, spriteBatch, this);
            cannon.Initialize();
            cannon.Fire(p1, is1Exploded, p2, is2Exploded);
        }
    }
}
